<script>
  var maximumSum = function (nums) {
    const obj = {};
    let ans = -1;
    nums.forEach(num => {
      const strNum = String(num);
      let count = 0;
      for (let i = 0; i < strNum.length; i++) {
        count += Number(strNum[i]);
      }
      const item = obj[count];
      if (item) {
        // item.push(num);
        if (item.length == 2) {
          item[1] = Math.max(item[1], num);
          ans = Math.max(ans, item[1] + item[0])
        } else {
          item.push(num);
          if (item.length == 2) {
            ans = Math.max(ans, item[1] + item[0])
          }
        }
      } else {
        obj[count] = [num];
      }
      obj[count].sort((a, b) => b - a);
    })
    return ans;
  };
  console.log(maximumSum([229, 398, 269, 317, 420, 464, 491, 218, 439, 153, 482, 169, 411, 93, 147, 50, 347, 210, 251, 366, 401]));
</script>